package datalookin.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.whu.zj.MyApplication;
import com.whu.zj.R;
import com.whu.zj.UIHandler;
import com.whu.zj.VolleySingleton;

import java.util.List;

import datalookin.adapter2.ChooseAdapter;
import main.activity.MainActivity;
import main.dto.NameToName;
import main.dto.WaterPlant;

/**
 *
 * 在枝江选择水厂的时候需要针对水厂进行重新获取name2name信息
 * Created by Administrator on 2016/6/27.
 */
public class ChooseActivity  extends UIHandler.BaseActivity{

    String waterplantCode;
    String waterplantName;

    ListView lv_choose;
    List<WaterPlant> waterPlants;
    ChooseAdapter chooseAdapter = new ChooseAdapter();
    {
        waterPlants = MyApplication.mobileUserDto.getWaterPlants();
        chooseAdapter.setWaterPlants(waterPlants);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chooseplant);
        initview();
    }

    private void initview() {
        lv_choose = (ListView) findViewById(R.id.lv_chooseplant);
        lv_choose.setAdapter(chooseAdapter);
        lv_choose.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                waterplantCode = waterPlants.get(i).getWaterPlantCode();
                waterplantName = waterPlants.get(i).getWaterPlantName();

                MyApplication.initRealPlant(waterplantCode);
//                get(waterplantCode);
                Intent intent = new Intent(ChooseActivity.this, MainWaterActivity2.class);
                intent.putExtra("waterPlantNum",waterplantCode);
                intent.putExtra("waterPlantName",waterplantName);
                ChooseActivity.this.startActivity(intent);
            }
        });
    }


    public void get(String plantNum) {
        String ip = MyApplication.share().getResources().getString(R.string.IP_zj);
        String url = "http://" + ip +  "/GetName2Name.action?plantNum="+plantNum;
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("水厂的站点数据", response);
                if (!response.startsWith("{"))
                {
                    Toast.makeText(MyApplication.share(),"获取失败",Toast.LENGTH_SHORT).show();
                    return;
                }
                Gson gson = new Gson();
                NameToName nametoname = gson.fromJson(response,NameToName.class);
                MyApplication.mobileUserDto.setNametoname(nametoname);

                Intent intent = new Intent(ChooseActivity.this, MainWaterActivity.class);
                intent.putExtra("waterPlantNum",waterplantCode);
                intent.putExtra("waterPlantName",waterplantName);
                ChooseActivity.this.startActivity(intent);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MyApplication.share(),"获取失败",Toast.LENGTH_SHORT).show();
            }
        });
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }
}
